<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="用户id" prop="memberId" >
        <a-input v-model="form.memberId" placeholder="请输入用户id" />
      </a-form-model-item>
      <a-form-model-item label="活动id" prop="activityId" >
        <a-input v-model="form.activityId" placeholder="请输入活动id" />
      </a-form-model-item>
      <a-form-model-item label="申请时长" prop="duration" >
        <a-input v-model="form.duration" placeholder="请输入申请时长" />
      </a-form-model-item>
      <a-form-model-item label="申请状态1通过2不通过" prop="status" >
        <a-radio-group v-model="form.status" button-style="solid">
          <a-radio-button value="">请选择字典生成</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="备注信息" prop="content" >
        <div id="content" name="content" ></div>
      </a-form-model-item>
      <a-form-model-item label="附件图片(一张)" prop="imgurl" >
        <a-input v-model="form.imgurl" placeholder="请输入附件图片(一张)" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getDurationApply, addDurationApply, updateDurationApply } from '@/api/angel/durationApply'
import Vditor from 'vditor'
import 'vditor/dist/index.css'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      loading: false,
      formTitle: '',
      contentEditor: '',
      // 表单参数
      form: {
        id: null,
        memberId: null,
        activityId: null,
        duration: null,
        status: null,
        content: null,
        imgurl: null
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    openEditor () {
      this.$nextTick(() => {
        this.contentEditor = new Vditor('content', {
          height: 360,
          toolbarConfig: {
            pin: true
          },
          cache: {
            enable: false
          },
          after: () => {
            let content = this.form.content
            if (content === undefined || content === null) {
              content = ''
            }
            this.contentEditor.setValue(content)
          }
        })
      })
    },
    closeEditor () {
      this.noticeContentEditor.destroy()
    },
    onClose () {
      this.open = false
      this.closeEditor()
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.closeEditor()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,
        memberId: null,
        activityId: null,
        duration: null,
        status: null,
        content: null,
        imgurl: null
      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.openEditor()
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getDurationApply(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
        this.openEditor()
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.content = this.contentEditor.getValue()
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id !== undefined && this.form.id !== null) {
            updateDurationApply(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.closeEditor()
              this.$emit('ok')
            })
          } else {
            addDurationApply(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.closeEditor()
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
